﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtMaNguoiDung.Text == "")
                MessageBox.Show("Xin hãy điền tên đăng nhập", "Error");
            else if (txtPassword.Text == "")
                MessageBox.Show("Xin hãy điền mật khẩu", "Error");
            else
            {
                _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
                _DB.NguoiDung NguoiDung = null;
                try
                {
                    NguoiDung = dc.NguoiDungs.Single(u => u.deleted == false && u.MaNguoiDung == txtMaNguoiDung.Text);
                }
                catch (System.InvalidOperationException ex)
                {}

                if (NguoiDung == null)
                    MessageBox.Show("Không tồn tại " + txtMaNguoiDung.Text, "Error");
                else if (NguoiDung.Password != txtPassword.Text)
                    MessageBox.Show("Sai mật khẩu", "Error");
                else
                {
                    Program.MaNguoiDungHienTai = NguoiDung.MaNguoiDung;
                    Program.NguoiDungHienTai = NguoiDung;
                    this.Close();
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OtherForms.SettingForm f = new OtherForms.SettingForm();
            f.ShowDialog();
        }
    }
}
